package com.zg.paging3.activity.net.other

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.LoadState
import androidx.paging.LoadStateAdapter
import androidx.recyclerview.widget.RecyclerView
import com.zg.paging3.activity.net.adapter.NetDataAdapter
import com.zg.paging3.databinding.HeaderLayoutBinding

/***
 * @Author:      zxd
 * @CreateDate:  2021/7/9 13:09
 * @Description: 这里的retry()方法，可以通过传进来的adapter，然后进行调用。也可以通过传入的参数为val retry: () -> Unit，调用
 * 这个LoadStateAdapter的使用，最好不要和SmartRefreshLayout一起使用
 */
class TestLoadStateAdapter(private val adapter: NetDataAdapter) :
    LoadStateAdapter<TestLoadStateAdapter.ViewHolder>() {

    class ViewHolder(
        val binding: HeaderLayoutBinding,
        val adapter: NetDataAdapter
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bindData(loadState: LoadState) {
            when (loadState) {
                is LoadState.Error -> {

                }
                is LoadState.Loading -> {
                    binding.root.visibility = View.VISIBLE
                    binding.root.setBackgroundColor(Color.BLUE)
                }
                is LoadState.NotLoading -> {
                    binding.root.visibility = View.VISIBLE
                    binding.root.setBackgroundColor(Color.BLUE)
                }
            }
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, loadState: LoadState) {
        holder.bindData(loadState)
    }

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): ViewHolder {
        val binding =
            HeaderLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding, adapter)
    }
}